package com.share.jedis;

import com.share.bean.Person;
import org.apache.commons.beanutils.BeanMap;
import org.junit.Before;
import org.junit.Test;
import org.redisson.Redisson;
import org.redisson.RedissonLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.redisson.connection.AddressResolverGroupFactory;
import redis.clients.jedis.*;

import java.util.*;

/**
 * author:caifan
 * date:2019/5/17
 */

public class JedisDemo_01 {
    Jedis jedis;

    @Before
    public void connect() {
        HostAndPort hostAndPort = new HostAndPort("127.0.0.1", 6379);
        jedis = new Jedis(hostAndPort);
        jedis.connect();
        /*JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(8);
        jedisPoolConfig.setMinIdle(8);
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPoolConfig.setMinIdle(0);
        */
    }

    @Test
    public void basicTest() {
        /*
         * setnx redis里没有对应的键则存入k v 返回1 否则返回0
         */
//        System.out.println(jedis.setnx("gender", "male"));
        System.out.println(jedis.setex("aaa", 10, "bbb"));//返回ok并存入kv，指定时间删除
        List<String> mgets = jedis.mget("abc", "name");
        mgets.stream().forEach(get ->  {
            System.out.println(get);
        });
    }

    @Test
    public void hashTest() throws Exception {
        Person person = new Person();
        person.setAge(20);
        Calendar calendar = Calendar.getInstance();
        calendar.set(1990, 2, 28);
        person.setBirth(calendar.getTime());
        person.setEmail("share@163.com");
        person.setName("分享");
        Map<String, String> personMap = new HashMap<>();
//        BeanUtils.populate(person, personMap); map to bean
        BeanMap beanMap = new BeanMap(person);
        for (Object key : beanMap.keySet()) {
            personMap.put(key.toString(), beanMap.get(key).toString());
        }
        //jedis.hmset("person", personMap);
        System.out.println(jedis.hmget("person", "name"));//[分享]
    }

    @Test
    public void listTest() {
        jedis.lpush("dbms", "mysql", "db2");
        jedis.lpush("dbms", "oracle", "sqlserver");
        List<String> dbms = jedis.lrange("dbms", 0, 10);
        System.out.println(dbms);
    }

    @Test
    public void setTest() {
        jedis.sadd("language", "java", "c语言", "C++");
        jedis.sadd("language", "python", "php", "kotlin");
        Set<String> language = jedis.smembers("language");
        System.out.println(language);
    }

    @Test
    public void zsetTest() {
        jedis.zadd("db_framework", 100, "mybaties");
        jedis.zadd("db_framework", 120, "hibernate");
        jedis.zadd("db_framework", 150, "tk_mybatis");
        jedis.zadd("db_framework", 80, "jdbc");
        Set<Tuple> tuples = jedis.zrangeByScoreWithScores("db_framework", 10, 120);
        Iterator<Tuple> iterator = tuples.iterator();
        while (iterator.hasNext()) {
            Tuple next = iterator.next();
            System.out.println(next.getScore() + ":" + next.getElement());
        }

        Set<String> stringSet = jedis.zrangeByScore("db_framework", 20, 150);
        System.out.println(stringSet);
    }

    public boolean lock(String key, String value, Integer time) {
        if (jedis.setnx(key, value) == 1) {
            jedis.expire(key, time);
            return Boolean.TRUE;
        }

        if (null != jedis.get(key)) {

        } else {
            jedis.setex(key, time, value);
        }
        return true;
    }


    @Test
    public void test() {
        System.out.println(new UUID(0l, 0l));
        Config config = new Config();
        /**
         * 集群配置
         */
        /*
        ClusterServersConfig clusterServersConfig = config.useClusterServers();
        clusterServersConfig.addNodeAddress("192.168.2.12:7001", "192.168.2.12:7002", "192.168.2.13:7001", "192.168.2.13:7002");
        */
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress("127.0.0.1:6379");
        RedissonClient redissonClient = Redisson.create(config);
        RLock lock = redissonClient.getLock("");
        lock.tryLock();
        lock.unlock();

    }
}
